
var request = require('request');
var rp = require('request-promise');

var serve = 'http://www.unicom-cloud.com:8082/web/api/wos/listWosOrderReAudit'
var list = []
function getWork() {
    var option = {
        url: serve,
        method: "POST",
        headers: {
            "content-type": "application/x-www-form-urlencoded;",
        },
        form: {
            apiKey: 'admin',
            timestamp: global.timestamp,
            sign: global.sign,
            accessType: 'MGR',
            status: 10,
            createTimeBegin: new Date().Format('yyyy-MM-dd') + ' 00: 00: 00',
            createTimeEnd: new Date().Format('yyyy-MM-dd') + ' 23: 59: 59',
            deployId: 'smsi_2',
            pageNum: 1,
            pageSize: 10,
            auditStart: 0,
        }
    }
    rp(option).then((body) => {
        var json = JSON.parse(body)
        if (json.data.list.length > 0) {
            var data = json.data.list
            data.forEach((e, i) => {
                var obj = new Object()
                obj.title = '工单审批 ' + e.entryName
                obj.messageURL = 'http://www.unicom-cloud.com:8081/pl/dsm/dashboard.html'
                obj.picURL = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201609%2F30%2F20160930135859_GRFEk.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614416553&t=dfb400ec87571dc78e9f6dcb5d17677e'
                list.push(obj)
            });
        }
        sendD(list)
        // console.log(list)
    })
}

function getContent() {
    var option = {
        url: 'http://www.unicom-cloud.com:8082/web/api/audit/listSmsContentVerify',
        method: "POST",
        headers: {
            "content-type": "application/x-www-form-urlencoded;",
        },
        form: {
            accessType: "MGR",
            apiKey: "admin",
            timestamp: global.timestamp,
            sign: global.sign,
            deployId: "smsi_2",
            cpId: "0",
            pageNum: 1,
            pageSize: 10,
            smsText: "",
            status: "10",
        }
    }
    rp(option).then((body) => {
        var json = JSON.parse(body)
        if (json.data.list.length > 0) {
            var data = json.data.list
            data.forEach((e, i) => {
                var obj = new Object()
                obj.title = '短信审批 ' + e.cpName
                obj.messageURL = 'http://www.unicom-cloud.com:8081/pl/dsm/dashboard.html'
                obj.picURL = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201609%2F30%2F20160930135859_GRFEk.jpeg&refer=http%3A%2F%2Fb-ssl.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1614416553&t=dfb400ec87571dc78e9f6dcb5d17677e'
                list.push(obj)
            });
        }
        getWork()
    })
}


function sendD(json) {
    var serve = 'https://oapi.dingtalk.com/robot/send?access_token=c2971694e55c2abe63be8f9e7236ab53207f17cb7e100e1a5dc6d954762724c8'
    var len = json.length >= 10 ? 10 : json.length

    var links = []
    for (var i = 0; i < len; i++) {
        links.push(json[i])
    }
    var datas = {
        "msgtype": "feedCard",
        "feedCard": {
            "links": links
        }
    }
    request({
        url: serve,
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        body: datas
    }, function (error, response, body) {
        // console.log(response)
        // console.log(body)
    })
}
function setIntervalFun() {
    list = []
    var nt1 = new Date().Format('hh')
    var nt2 = new Date().Format('mm')
    var nt3 = (Number(nt1) * 60) + Number(nt2)

    var week = new Date().getDay()
    if (week == 0 || week == 6) {
        if (480 < nt3 && nt3 < 1350) {
            getContent()
        }
    } else {
        if (1080 < nt3 && nt3 < 1350) {
            getContent()
        }
    }
}

module.exports = {
    setIntervalFun
}